/*****************************************************************************
* File Name: AudioControl.h
* Version 
*
* Description:  This file contains the Audio signal path configuration routine
*                declarations and constants.
*
* Owner:
* SIRK
*    
* Related Document:
*     
*
* Hardware Dependency:
* CYPRESS BLE Dongle
*
* Code Tested With:
* PSoC Creator    
*
******************************************************************************
* Copyright (2015), Cypress Semiconductor Corporation.
******************************************************************************
* This software is owned by Cypress Semiconductor Corporation (Cypress) and is
* protected by and subject to worldwide patent protection (United States and
* foreign), United States copyright laws and international treaty provisions.
* Cypress hereby grants to licensee a personal, non-exclusive, non-transferable
* license to copy, use, modify, create derivative works of, and compile the
* Cypress Source Code and derivative works for the sole purpose of creating
* custom software in support of licensee product to be used only in conjunction
* with a Cypress integrated circuit as specified in the applicable agreement.
* Any reproduction, modification, translation, compilation, or representation of
* this software except as specified above is prohibited without the express
* written permission of Cypress.
*
* Disclaimer: CYPRESS MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH
* REGARD TO THIS MATERIAL, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
* Cypress reserves the right to make changes without further notice to the
* materials described herein. Cypress does not assume any liability arising out
* of the application or use of any product or circuit described herein. Cypress
* does not authorize its products for use as critical components in life-support
* systems where a malfunction or failure may reasonably be expected to result in
* significant injury to the user. The inclusion of Cypress' product in a life-
* support systems application implies that the manufacturer assumes all risk of
* such use and in doing so indemnifies Cypress against all charges. Use may be
* limited by and subject to the applicable Cypress software license agreement.
*****************************************************************************/
#ifndef _AUDIOCONTROL_H_
#define _AUDIOCONTROL_H_ 
#include <project.h>
#include <stdbool.h>
 
/*****************************************************************************
* MACRO Definition
*****************************************************************************/

/*Constants for USB descriptors */
#define AUDIO_IN_ENDPOINT          USBFS_EP3
#if defined(USBFS_ENABLE_CDC_CLASS)
#define AUDIO_IN_INTERFACE_NO      (5u)
#else
#define AUDIO_IN_INTERFACE_NO      (3u)
#endif

#define AUDIO_SMALL_SIZE_PKT
//#define AUDIO_NO_HEADER
//#define AUDIO_REPEAT_LAST_SAMPLE

/*Number of bytes for each 1ms sample*/
#define AUDIO_SAMPLE_SIZE          (32u) /*16KHz 16bit sampling rate */

/*Number of 1ms samples to be stored in circular jitter buffer - This size will effect the initial latency of the audio transfer*/
#define JITTER_BUFFER_SIZE_IN_MS   (200u)
#define TX_RETRY_BUFFER_SIZE_IN_MS (150u)

#define AUDIO_SYNC_SIZE            (3u)
#define IN_BUFSIZE                 ((AUDIO_SAMPLE_SIZE) * (JITTER_BUFFER_SIZE_IN_MS))

/*Make sure IN_SENDLIMIT/IN_TRANS_SIZE is integer which will be initial TD index*/
#define IN_SENDLIMIT               ((AUDIO_SAMPLE_SIZE) * (TX_RETRY_BUFFER_SIZE_IN_MS))

#ifdef AUDIO_SMALL_SIZE_PKT

    #ifdef AUDIO_NO_HEADER
    #define AUDIO_DATA_SIZE        (19u)
    #define IN_TRANS_SIZE          (64u)
    #else
    #define AUDIO_DATA_SIZE        (20u)
    #define IN_TRANS_SIZE          (80u)
    #endif
    #define NUM_TDS                (IN_BUFSIZE/IN_TRANS_SIZE)

#else

    #ifdef AUDIO_NO_HEADER
    #define AUDIO_DATA_SIZE        (83u)
    #else
    #define AUDIO_DATA_SIZE        (80u)
    #endif
    #define IN_TRANS_SIZE          (320u)
    #define NUM_TDS                (IN_BUFSIZE/IN_TRANS_SIZE)

#endif /*AUDIO_SMALL_SIZE_PKT*/

/*****************************************************************************
* Global Variable Declaration
*****************************************************************************/
extern bool inPlaying;

extern uint8 inBuffer[IN_BUFSIZE];
extern uint8 inTransBuffer[IN_TRANS_SIZE];
/*Flag to indicate audio pipe opening and closing*/
extern volatile bool audioPipeOpen;
/*This flag indicates the DMA transaction completion(Decompressed Audio Pkt) 
 *from inTransBuffer to circular buffer.*/
extern volatile bool RxDMADone;
extern volatile uint16 inLevel;    /* audio IN buffer level pointer */

/*****************************************************************************
* External Function Prototypes
*****************************************************************************/          

/*******************************************************************************
* Function Name: ConfigureAudioPath
********************************************************************************
* Summary:
*       This function sets up the DMA and starts USB and interrupts
*       to get the PSoC 5 device configured for audio streaming mode
*
* Parameters:
*  None
*
* Return:
*  None
*
* Theory:
*  One TD configured for one input transaction into circular buffer, every trigger for RXDMA will use the next configured TD and load the Circ buffer.
*
* Side Effects:
* None
*
*******************************************************************************/
extern void ConfigureAudioPath(void);

/*******************************************************************************
* Function Name: ProcessAudioIn
********************************************************************************
* Summary:
*        Handle USB audio in data, setup USB DMA and trigger to transfer samples 
*        from SRAM audio circular buffer to SRAM USB endpoint memory.
*        Enable RXDMA channel to load the RX data to circ buffer when occurs.
*
* Parameters:
*  None
*
* Return:
*  None
*
* Theory:
*  On each USB poll for Audio EP, one sample data will be fethed from Circ buffer via InDma and loaded to EP.
*
* Side Effects:
* Incremenets Circ Buffer read pointer inBufIndex
* inPlaying will be set to true once the data transfer to Host is started
*
*******************************************************************************/
extern void ProcessAudioIn(void);

/*******************************************************************************
* Function Name: HandleAudioInDisable
********************************************************************************
* Summary:
*       This routine clears the audio IN stream circular buffer and the audio
*       IN endpoint memory location based on the status of the audio IN stream.
*
* Parameters:
*  None
*
* Return:
*  None
*
* Theory:
*  When hosts sends Config request to change the alternate interface, the audio data path need to be disabled as USB is not polling for data.
*  Disable the RX DMA and Clear the buffers, RX data is expected to be dropped after this command from Host
*
* Side Effects:
*  inPlaying will be set to false to indicate that new data to be dropped
*  Circ buffer and corresponding pointers will be reset
*
*******************************************************************************/
extern void HandleAudioInDisable(void);

/*******************************************************************************
* Function Name: Trigger_rXDMA
********************************************************************************
* Summary:
*       This routine sets the request to rxDMA that transfers data from RX buffer to circular buffer. 
*       This API should be called after the RX packet is ready. Caller is expected to wait until current DMA transaction is completed.
*       This function also check for Circ buffer over flow condition and drops new data if no available space in the buffer.
*
* Parameters:
*  None
*
* Return:
*  None
*
* Theory:
*  None
*
* Side Effects:
*  RxDMADone will be set to false which needs to be set to true once the active DMA is completed
*  If no space available in the buffer DMA will not be triggered.
*
*******************************************************************************/
extern void Trigger_rXDMA(void);

/*******************************************************************************
* Function Name: HandleUnderflow
********************************************************************************
* Summary:
*       This routine monitors the amount of valid data available in the inBuffer
*       and handles the underflow condition when the data in inBufer is less
*       then IN_UNDERFLOWLIMIT 
* Parameters:
*  None
*
* Return:
*  None
*
* Theory:
*  USB ISOC EP expects data on every polling, if no valid data available in the buffer, this function creates virtual data and sends it to Host
*
* Side Effects:
*  RxDMADone will be set to false which needs to be set to true once the active DMA is completed
*  If no space available in the buffer DMA will not be triggered.
*
*******************************************************************************/
extern void HandleUnderflow(void);


/*******************************************************************************
* Function Name: ServiceUSB
********************************************************************************
* Summary: 
*          When the USB configuration is changed, this routine reinitializes
*          USB audio endpoints as required by the application.
*
* Parameters:
*  None
*
* Return:
*  None
*
* Theory:
*  None
*
* Side Effects:
*  None
*
*******************************************************************************/
extern void ServiceUSB(void);

#endif /*_AUDIOCONTROL_H_*/
/* [] END OF FILE */

